<html>
<head><meta charset="utf-8"><title>closure DefId from HirId · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html">closure DefId from HirId</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215828951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215828951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215828951">(Nov 06 2020 at 10:42)</a>:</h4>
<p>Hey all. I am trying to link an attribute to a closure but can't use <code>#[feature(stmt_expr_attributes)]</code>. In proc_macros we generate something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="cp">#[some_attr]</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">_closure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|..</span><span class="p">.</span><span class="o">|</span><span class="w"> </span><span class="o">..</span><span class="p">.;</span><span class="w"></span>
<span class="w">  </span><span class="n">_closure</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>If <code>#[some_attr]</code> is directly on the closure, the attribute gets attached to the actual implementation function, but as shown above, the attribute is only on the assignment statement. I can find this <code>#[some_attr]</code> when walking the HIR, I find it in a <code>Local</code> statement. I was wondering if I can somehow turn the <code>HirId</code>(s) that I find in that <code>Local</code> into a <code>DefId</code> of the implementation function of the closure?</p>



<a name="215851857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215851857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215851857">(Nov 06 2020 at 14:22)</a>:</h4>
<p>DefIds are cross-crate and I don't think closures are, so it may not be possible</p>



<a name="215851940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215851940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215851940">(Nov 06 2020 at 14:23)</a>:</h4>
<p>but if it is, you'd do it with <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.local_def_id"><code>local_def_id()</code></a></p>



<a name="215858981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215858981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215858981">(Nov 06 2020 at 15:13)</a>:</h4>
<p>Thanks, I think that did it! <code>local_def_id</code> returned the closure <code>DefId</code> when I called it on the closure's <code>body.hir_id</code>. What did you mean by the cross-crate comment? The closure I have is always defined right there in the assignment statement.</p>



<a name="215861484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215861484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215861484">(Nov 06 2020 at 15:30)</a>:</h4>
<p>I think closures can be cross-crate though</p>



<a name="215862002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215862002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215862002">(Nov 06 2020 at 15:34)</a>:</h4>
<p>DefIds are a stable identifier that can be used across crates, when compiling downstream crates that depend on the current one. Not everything that has a HirId has a DefId, for example <code>use</code> statements.</p>



<a name="215862030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/closure%20DefId%20from%20HirId/near/215862030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/closure.20DefId.20from.20HirId.html#215862030">(Nov 06 2020 at 15:35)</a>:</h4>
<p>It sounds like closures do have DefIds though</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>